package com.zhentao.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;
@Component
public class UserUtils {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public int GetUserId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String header = request.getHeader("Authorization");
        String data = stringRedisTemplate.opsForValue().get("login:data:" + header);
        Map<String, Object> map = JwtService.getClaimsMap(data);
        Integer  id = Integer.valueOf(map.get("userId").toString());
        return id;
    }
}
